<template>
	<view class="">
		<view class="uni-popup-dialog  ">

			<view class="uni-dialog-title">
				<text class="f-18 f-w-800" style="color: #000;">{{find.up_name}}</text>
			</view>
			<view class="banmi_flex j-s p-20">
				<view class="">
					<text class="f-15">当前版本</text>
					<text class="f-15" style="color: #228B22;">v{{system_version}}</text>
				</view>
				<view class="">
					<text class="f-15">最新版本</text>
					<text class=" f-15 ml1" style="color: #228B22;">v{{find.name}}</text>
				</view>
			</view>
			<view v-if="is_up" class="p-20">
				<template v-if="log=='更新完成,已是最新版本'">
					<view class="p2 banmi_flex j-c w800" style="color: #409eff;">
						{{log}}
					</view>
					<view class="p2 banmi_flex j-c  " style="padding-bottom: 150rpx;">
						<view @click="on_url()" class="banmi_bt_1 banmi_flex j-c a-c"
							style="width: 600rpx;height: 40px;">
							<text>重启网页</text>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="p2 banmi_flex j-c f-w-800" style="color: #409eff;">
						{{log}}
					</view>
					<ProgressBar :size="size" :issize="is_size" :percentage="percentage" Type="aqua" :Vice="false">
					</ProgressBar>
					<view class="p2 banmi_flex j-c cred f-w-800  pt3" style="padding-bottom: 100px;">
						请勿关闭此窗口
					</view>
				</template>
			</view>
			<view class="p-20  " v-if="!is_up">
				<scroll-view scroll-y="true" style="height: 300px;">
					<view class="p-l-r-10 ">
						<text class="p-l-r-10 f-w-800">v{{find.name}}更新说明</text>
						<view class="p-10">

						</view>
						<rich-text class="f-15 l-25 " style="color: #333333;" :nodes="find.content"></rich-text>
					</view>
				</scroll-view>
			</view>
			<template v-if="!is_up">
				<view v-if="find.update==1" class="banmi_flex j-s p-20">
					<view v-if="find.type==0" @click="close">
						<button class="banmi_bt_1 banmi_flex j-c a-c" style="width: 100px;height: 30px;">
							<text class="f-15">暂时不更新</text>
						</button>
					</view>
					<view @click="on_up">
						<button class="banmi_bt_1 banmi_flex j-c a-c" style="width: 100px;height: 30px;">
							<text class="f-15">一键更新</text>
						</button>
					</view>
				</view>
				<view v-if="find.update==0" class="p-20">
					<view class="   " @click="close">
						<button class="banmi_bt_1 banmi_flex j-c a-c" style="width: 100px;height: 30px;">
							<text class="f-15">关闭</text>
						</button>
					</view>
				</view>

			</template>

		</view>
	</view>
</template>

<script>
	import popup from '../uni-popup/popup.js'
	import {
		initVueI18n
	} from '@dcloudio/uni-i18n'
	import messages from '../uni-popup/i18n/index.js'
	const {
		t
	} = initVueI18n(messages)
	/**
	 * PopUp 弹出层-对话框样式
	 * @description 弹出层-对话框样式
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=329
	 * @property {String} value input 模式下的默认值
	 * @property {String} placeholder input 模式下输入提示
	 * @property {String} type = [success|warning|info|error] 主题样式
	 *  @value success 成功
	 * 	@value warning 提示
	 * 	@value info 消息
	 * 	@value error 错误
	 * @property {String} mode = [base|input] 模式、
	 * 	@value base 基础对话框
	 * 	@value input 可输入对话框
	 * @property {String} content 对话框内容
	 * @property {Boolean} beforeClose 是否拦截取消事件
	 * @event {Function} confirm 点击确认按钮触发
	 * @event {Function} close 点击取消按钮触发
	 */
	import ProgressBar from '@/components/ai-progress/ai-progress.vue';

	export default {
		name: "uniPopupDialog",
		mixins: [popup],
		emits: ['confirm', 'close'],
		components: {

			ProgressBar,

		},
		props: {
			issousuo: {
				type: String,
				default () {
					return '';
				}
			},

			name: {
				type: [String, Number],
				default: ''
			},
			find: {
				type: Object,
				default () {
					return {};
				}
			},
			isvip: {
				type: [String, Number],
				default: ''
			},
			placeholder: {
				type: [String, Number],
				default: ''
			},
			type: {
				type: String,
				default: 'error'
			},
			mode: {
				type: String,
				default: 'base'
			},
			title: {
				type: String,
				default: ''
			},
			content: {
				type: String,
				default: ''
			},
			beforeClose: {
				type: Boolean,
				default: false
			},
			cancelText: {
				type: String,
				default: ''
			},
			confirmText: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				dialogType: 'error',
				focus: false,
				val: "",
				tabidnex: 1,
				zd_price: 0,
				text: '',
				is_up: false,
				size: '',
				is_size: 0,
				up_find: {},
				percentage: 0,
				log: '正在下载',
				system_version: ''
			}
		},
		computed: {

			okText() {
				return this.confirmText || t("uni-popup.ok")
			},
			closeText() {
				return this.cancelText || t("uni-popup.cancel")
			},
			placeholderText() {
				return this.placeholder || t("uni-popup.placeholder")
			},
			titleText() {
				return this.title || t("uni-popup.title")
			}
		},
		watch: {
			type(val) {
				this.dialogType = val
			},
			mode(val) {
				if (val === 'input') {
					this.dialogType = 'info'
				}
			},
			value(val) {
				this.val = val
			}
		},
		created() {
			this.system_version = uni.getStorageSync('system_version');


			// 对话框遮罩不可点击
			this.popup.disableMask()
			// this.popup.closeMask()
			if (this.mode === 'input') {
				this.dialogType = 'info'
				this.val = this.value
			} else {
				this.dialogType = this.type
			}
		},
		mounted() {
			this.focus = true

		},
		methods: {
			on_url() {
				var url = window.location.href;
				var homeurl = getCaption(url, 0);

				window.location.href = homeurl;


				function getCaption(obj, state) {
					var index = obj.lastIndexOf("#");
					if (state == 0) {
						obj = obj.substring(0, index);
					} else {
						obj = obj.substring(index + 1, obj.length);
					}
					return obj;
				}
			},
			on_up() {

				var that = this;
				this.is_up = true;
				this.Http.Up_update_ajax().then(ret => {
					if (ret.status == 200) {
						this.up_find = ret.data;

						this.log = '正在下载' + this.up_find.v + '版本';

						var size = this.up_find.fileSize / 1024 / 1024;
						this.size = Math.trunc(size);
						that.on_up_xia_ajax();
						this.timer = setInterval(() => {
							that.on_up_ajax();
						}, 1000);
						return
					}
					this.log = '更新完成,已是最新版本';
					uni.showToast({
						title: ret.msg,
						icon: 'none',
					})




				})

			},
			on_up_ajax() {

				this.Htpp.Up_update_zip_size_ajax(this.up_find.zip_name).then(ret => {

					var find = ret.data;
					var issize = find.size / 1024 / 1024;
					issize = issize.toFixed(2);

					this.is_size = issize;
					var num = find.size / this.up_find.fileSize * 100;

					// num = Math.trunc(num);
					this.percentage = num;
				})
			},
			on_up_xia_ajax() {

				this.Http.Up_update_update_ajax_ajax().then(ret => {
					clearInterval(this.timer);
					if (ret.status == 200) {
						this.percentage = 100;
						this.log = '正在安装';
						var path = ret.data.path;
						var toPath = ret.data.toPath;
						var v = ret.data.v;
						this.on_up_install_ajax(path, toPath, v);

					} else {
						uni.showToast({
							title: ret.msg,
							icon: 'error'
						})
					}

				})
			},
			on_up_install_ajax(path, toPath, v) {

				this.Htpp.Up_zip_install_ajax(toPath, path, v).then(ret => {


					if (ret.status == 200) {
						this.system_version = this.up_find.v;
						uni.setStorage({
							key: 'system_version',
							data: this.system_version
						});
						this.log = this.up_find.v + '安装完成，正在检测是否还有新版';
						this.on_up();
						return

					}
					uni.showToast({
						title: ret.msg,
						icon: 'error'
					})

				})
			},
			/**
			 * 点击取消按钮
			 */
			closeDialog() {
				this.$emit('close')
				if (this.beforeClose) return
				this.popup.close()
			},
			close() {
				this.popup.close()
			}
		}
	}
</script>


<style lang="scss">
	.classify-liv {
		text-align: center;
		align-items: center;
		padding-bottom: 10px;
		border: solid 2px #FD9A01;
		text-align: center;
		color: #999;
		border-radius: 10px;
		width: 200px;
		font-size: 24upx;
		margin: 10;

	}

	.classify-lixzlv {
		text-align: center;
		align-items: center;
		padding-bottom: 10px;
		border: solid 2px #999;

		color: #999;
		border-radius: 10px;
		width: 200px;
		font-size: 24upx;
		margin: 10;

	}

	@media screen and (min-width: 500px) {
		.uni-popup-dialog {
			width: 550px;
			border-radius: 11px;
			background-color: #fff;
		}
	}

	@media screen and (max-width: 500px) {
		.uni-popup-dialog {
			width: 320px;
			border-radius: 11px;
			background-color: #fff;
		}
	}

	.uni-dialog-title {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding-top: 25px;
	}

	.uni-dialog-title-text {
		font-size: 16px;
		font-weight: 500;
	}

	.uni-dialog-content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding-top: 10px;
	}

	.uni-dialog-content-text {
		font-size: 14px;
		color: #6C6C6C;
	}

	.f20 {
		font-size: 24px;
	}

	.uni-dialog-button-group {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		border-top-color: #f5f5f5;
		border-top-style: solid;
		border-top-width: 1px;
	}

	.uni-dialog-button {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */

		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 45px;
	}

	.uni-border-left {
		border-left-color: #f0f0f0;
		border-left-style: solid;
		border-left-width: 1px;
	}

	.uni-dialog-button-text {
		font-size: 16px;
		color: #333;
	}

	.uni-button-color {
		color: #007aff;
	}

	.uni-dialog-input {
		flex: 1;
		font-size: 14px;
		border: 1px #eee solid;
		height: 40px;
		padding: 0 10px;
		border-radius: 5px;
		color: #555;
	}

	.uni-popup__success {
		color: #4cd964;
	}

	.uni-popup__warn {
		color: #f0ad4e;
	}

	.uni-popup__error {
		color: #dd524d;
	}

	.uni-popup__info {
		color: #909399;
	}
</style>